package com.argrace.smart.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.argrace.smart.R;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;

import java.util.ArrayList;

/**
 * Created by argrace_dev_1 on 2018/2/26.
 */

public class RuoQi_DeviceTypeListActivity extends BaseActivity {
    private ListView listView;
    private DeviceTypeListAdapter adapter;
    private int type = 0;
    private boolean is_multiplechoice = false;//默认是否多选
    public ArrayList<String> allPickS = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ruoqi_devicetypelist);
        init();

    }

    private void init() {
        setTitle(true, false, "我的若琪");
        showRight(true, R.mipmap.head_title_tick);
        is_multiplechoice = getIntent().getBooleanExtra("is_multiplechoice",false);
        setLeftandRightClickListeners(null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!is_multiplechoice && allPickS.size() > 1) {
                    toastActBtm("只能选择一个类型");
                    return;
                }
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < allPickS.size(); i++) {
                    sb.append(allPickS.get(i)).append(",");
                }
                sb.deleteCharAt(sb.length() - 1);
                Log.i("info", "选中的数据是" + sb.toString());
                Intent intent = new Intent();
                intent.putExtra("result", sb.toString());
                setResult(ConstantConfigure.RESULT_SUCCESS, intent);
                finish();
            }
        });
        setView();
    }

    private void setView() {
        listView = (ListView) findViewById(R.id.activity_ruoqi_devicetypelist_listview);
        type = getIntent().getIntExtra("type", 0);
        switch (type) {
            case ConstantConfigure.RUOQI_REQUESTTYPE_DEVICETYPE:
                is_multiplechoice = false;
                adapter = new DeviceTypeListAdapter(RuoQi_DeviceTypeListActivity.this, ConstantConfigure.RUOQI_DEVICETYPE, is_multiplechoice);
                break;
            case ConstantConfigure.RUOQI_REQUESTTYPE_ACTIONS:
                is_multiplechoice = true;
                adapter = new DeviceTypeListAdapter(RuoQi_DeviceTypeListActivity.this, ConstantConfigure.RUOQI_DEVICE_ACTIONS, is_multiplechoice);
                break;
        }
        listView.setAdapter(adapter);

    }

    class DeviceTypeListAdapter extends BaseAdapter {
        String[] datas;
        private Context context;
        private boolean is_multiplechoice = false;

        DeviceTypeListAdapter(Context context, String[] datas, boolean is_multiplechoice) {
            this.context = context;
            this.datas = datas;
            this.is_multiplechoice = is_multiplechoice;
        }

        @Override
        public int getCount() {
            return datas.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_ruoqi_devicetype, parent, false); //加载布局
                holder = new ViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.item_ruoqi_devicetype_type_text);
                holder.ll = (LinearLayout) convertView.findViewById(R.id.item_ruoqi_devicetype_type_ll);
                holder.img = (ImageView) convertView.findViewById(R.id.item_ruoqi_devicetype_type_img);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            String[] str_split = datas[position].split(":");
            final ViewHolder finalholder = holder;
            holder.ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //如果是单选的，则清空其他选项
                    if (finalholder.img.getVisibility() == View.VISIBLE) {
                        finalholder.img.setVisibility(View.GONE);
                        allPickS.remove(datas[position]);
                    } else {
                        finalholder.img.setVisibility(View.VISIBLE);
                        allPickS.add(datas[position]);
                    }
                }
            });
            holder.name.setText(str_split[1]);
            return convertView;
        }

        class ViewHolder {
            TextView name;
            ImageView img;
            LinearLayout ll;
            boolean is_choice = false;
        }
    }


}
